package cn.seamus.marksman.task.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.seamus.marksman.common.Constants;
import cn.seamus.marksman.common.MarksmanException;
import cn.seamus.marksman.processor.Processor;
import cn.seamus.marksman.processor.impl.ProcessResult;
import cn.seamus.marksman.task.Task;

/**
 * 
 * 
 * <p>
 * 默认任务：添加一个商品到购物车并下单支付 <br/>
 * <br/>
 * 流程：
 * <ul>
 * <li>登录
 * <li>打开商品页
 * <li>加入指定数量到购物车
 * <li>Check out 1
 * <li>Check out 2
 * <li>支付
 * <li>结束
 * </ul>
 * </p>
 * 
 * @author zhengss
 * @version 0.0.1
 */
public class DefaultTask implements Task {
    private static Log logger = LogFactory.getLog(DefaultTask.class);

    private String username;

    private String goodsPageUrl;

    private String goodsQuantity;

    @Resource(name = "loginProcessor")
    private Processor loginProcessor;

    @Resource(name = "commonPageProcessor")
    private Processor commonPageProcessor;

    @Resource(name = "goodsPageProcessor")
    private Processor goodsPageProcessor;

    @Resource(name = "cartPageProcessor")
    private Processor cartPageProcessor;

    @Resource(name = "checkOut1PageProcessor")
    private Processor checkOut1PageProcessor;

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username
     *            the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public void process() {
        logger.info("检查登录");

        boolean isLogin = false;
        logger.info("username=" + username);
        logger.info("检查登录结束");

        do {
            logger.info("开始登录");
            Map<String, Object> requestParams = new HashMap<String, Object>();
            try {
                ProcessResult result = loginProcessor.process(requestParams);

                isLogin = result.getNavigator() != null;

            } catch (MarksmanException e) {
                logger.error(e);
            }

            if (!isLogin) {
                logger.info("登录未成功，准备重试");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e1) {
                    logger.error(e1);
                }
            }
        } while (!isLogin);

        logger.info("登录成功");
        logger.info("加入购物车开始");
        try {
            Map<String, Object> requestParams = new HashMap<String, Object>();
            requestParams.put(Constants.MAP_KEY_NEXT_PAGE, goodsPageUrl);
            requestParams.put(Constants.MAP_KEY_QUANTITY, this.goodsQuantity);
            goodsPageProcessor.process(requestParams);
        } catch (MarksmanException e) {
            logger.error(e);
        }
        logger.info("加入购物车结束");

        logger.info("打开购物车开始");
        ProcessResult result = null;
        try {
            result = cartPageProcessor.process(null);
        } catch (MarksmanException e) {
            logger.error(e);
        }
        logger.info("打开购物车结束");

        logger.info("输入账单地址与收货地址开始");
        try {
            checkOut1PageProcessor.process(result == null ? null : result.getParams());
        } catch (MarksmanException e) {
            logger.error(e);
        }
        logger.info("输入账单地址与收货地址结束");

        logger.info("任务完成");
    }

    public String getGoodsPageUrl() {
        return goodsPageUrl;
    }

    public void setGoodsPageUrl(String goodsPageUrl) {
        this.goodsPageUrl = goodsPageUrl;
    }

    public String getGoodsQuantity() {
        return goodsQuantity;
    }

    public void setGoodsQuantity(String goodsQuantity) {
        this.goodsQuantity = goodsQuantity;
    }
}
